package exception;

import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * @Author: ycy
 * @CreateDate: 2020/5/20 17:12
 */
public class ExceptionDemo {

  public static void main(String[] args) {
    try {
      System.out.println(2 / 0);
    } catch (Exception e) {
//      System.out.println(stackTrace(e));
      throw new RuntimeException(e);
    }
  }

  public static String stackTrace(Throwable e) {
    StringWriter stackTraceStringWriter = new StringWriter();
    PrintWriter printWriter = new PrintWriter(stackTraceStringWriter);
    e.printStackTrace(printWriter);
    return stackTraceStringWriter.toString();
  }

  public void doWork() {
    /**
     *     RuntimeException 不需要捕获
     */
    doWork2();

    /**
     *     RuntimeException 必须要捕获
     */
    try {
      doWork1();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public void doWork1() throws Exception {

  }

  public void doWork2() throws RuntimeException {

  }

}
